package com.example.jpa.demo_jpa.controller;

import com.example.jpa.demo_jpa.model.AyUser;
import com.example.jpa.demo_jpa.model.PhoneType;
import com.example.jpa.demo_jpa.service.AyUserService;
import com.example.jpa.demo_jpa.service.PhoneTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HelloController {
    @Autowired
    AyUserService myService;
    @Autowired
    PhoneTypeService phoneTypeService;
    @Resource
    private RedisTemplate<String,String> redisT;

    @RequestMapping("/hello")
    public String First() {

        redisT.opsForValue().set("name","ay");
        String name=(String)redisT.opsForValue().get("name");

        redisT.opsForList().leftPush("listname","list1");
        redisT.opsForList().leftPush("listname","list2");
        redisT.opsForList().leftPush("listname","list3");
        redisT.opsForList().leftPush("listname","list4");
        String str="| ";
        List<String> strL=redisT.opsForList().range("listname",0,-1);
        for(String a : strL){
            str +=a +" | ";
        }
        return myService.findAll().get(0).getName()+" == "+name +" == " + str;
    }

    @RequestMapping("/failed")
    public Map<String, String> requestFailed(){

        Map<String, String> map = new HashMap<>();
        map.put("code", "-1");
        map.put("msg", "请求错误");
        return map;
    }

    @RequestMapping("/success/info")
    public AyUser userInfo(){
        AyUser user = new AyUser();
        user.setName("xiaodong");
        user.setPassword("123456");
        user.setId("2020");
        return user;
    }

    @RequestMapping("/getuserbyname")
    public AyUser getuserbyname(){
        return myService.findbyName("libai");
    }

    @RequestMapping("/findByNumber")
    public PhoneType findByNumber(String number){
        return phoneTypeService.findByNumber("131");
    }

}
